package com.cuizhang.demo;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.util.FloatMath;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.ll.core.magzine.model.bean.Article;
import com.ll.core.util.CommonTool;
import com.ll.core.util.JSONTool;
import com.ll.core.util.TypeTokens;
import com.ll.core.view.util.ViewUtil;

public class ArticlePage extends Activity implements OnTouchListener {

	float oldDist;

	// 模式
	static final int NONE = 0;
	static final int DRAG = 1;
	static final int ZOOM = 2;
	int mode = NONE;
	float textSize = 14;
	

	private String imagename;
	private Article article;
	private final static int DIALOG_SHARE = 100;
	private TextView contentView;
	private TextView publishView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.home_page_main);

		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_text_share);
		((TextView) findViewById(R.id.title)).setText("新闻");

		setContentView(R.layout.article_page);
		article = (Article)JSONTool.stringToObject(getIntent().getStringExtra("article"), TypeTokens.ARTICLE);
		
		String titleStr = article.getTitle();
		String contentStr = article.getContent();
		String publisherStr = CommonTool.DateToString(article.getPublish_date(), "yyyy年MM月dd日 hh:mm:ss")+"   "+article.getPublisher();
		imagename = article.getImage_id();

		if (imagename != null) {
			ImageView imageView = (ImageView) findViewById(R.id.stage_image);
			imageView.setImageBitmap(ViewUtil.loadImage(imagename));
			//imageView.setImageDrawable(getResources().getDrawable(imageId));
			imageView.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					Intent i = new Intent(ArticlePage.this, ImageActivity.class);
					i.putExtra("imagename",imagename);
					startActivity(i);
				}
			});
		} else {
			ImageView imageView = (ImageView) findViewById(R.id.stage_image);
			imageView.setVisibility(View.INVISIBLE);
		}

		TextView title = (TextView) findViewById(R.id.stage_title);
		title.setText(titleStr);

		contentView = (TextView) findViewById(R.id.content);
		contentView.setText(Html.fromHtml(contentStr));
		contentView.setOnTouchListener(this);
		textSize = contentView.getTextSize();
		publishView = (TextView) findViewById(R.id.stage_publisher);
		publishView.setText(publisherStr);

		Button btn = (Button) findViewById(R.id.back);
		btn.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				finish();
			}
		});

		Button shareButton = (Button) findViewById(R.id.share);
		shareButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				showDialog(DIALOG_SHARE);
			}
		});

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		LayoutInflater factory = LayoutInflater.from(this);
		switch (id) {
		case DIALOG_SHARE:
			final View groupRename = factory.inflate(R.layout.share, null);
			View v1 = groupRename.findViewById(R.id.title1);
			View v2 = groupRename.findViewById(R.id.title2);
			View v3 = groupRename.findViewById(R.id.title3);
			View v4 = groupRename.findViewById(R.id.title4);
			v1.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					removeDialog(DIALOG_SHARE);
				}
			});
			v2.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					removeDialog(DIALOG_SHARE);
				}
			});
			v3.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					removeDialog(DIALOG_SHARE);
				}
			});
			v4.setOnClickListener(new View.OnClickListener() {

				public void onClick(View v) {
					removeDialog(DIALOG_SHARE);
				}
			});

			AlertDialog renameDialog = new AlertDialog.Builder(ArticlePage.this).setTitle("分享").setView(groupRename).create();
			return renameDialog;
		}
		return super.onCreateDialog(id);
	}

	static final String TAG = "ActivitySwipeDetector";

	public boolean onTouch(View v, MotionEvent event) {

		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		case MotionEvent.ACTION_DOWN:
			mode = DRAG;
			break;
		case MotionEvent.ACTION_POINTER_DOWN:
			mode = ZOOM;
			oldDist = spacing(event);
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
			mode = NONE;
			break;
		case MotionEvent.ACTION_MOVE:
			 if (mode == ZOOM) {
				float newDist = spacing(event);
				if (newDist > oldDist && textSize < 35) {
					textSize++;
				}else if(newDist < oldDist && textSize > 10){
					textSize--;
				}
			}
			break;
		}
		contentView.setTextSize(textSize);

		return true;
	}


	// 计算移动距离
	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}


}
